package cn.wx.scholar.neo4j.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import cn.wx.common.persistence.exception.MyException;
import cn.wx.common.persistence.web.ResponseCode;
import cn.wx.scholar.core.entity.ScholarPatents;
import cn.wx.scholar.core.entity.qo.ScholarPatentsQ;
import cn.wx.scholar.core.service.ScholarPatentsService;
import cn.wx.scholar.neo4j.entity.Neo4jPatents;
import cn.wx.scholar.neo4j.entity.qo.Neo4jPatentsQ;
import cn.wx.scholar.neo4j.repository.PatentsRepository;

@Service
public class Neo4jPatentsService {
	
	Logger logger = LoggerFactory.getLogger(Neo4jPatentsService.class);
	
	@Autowired
    private PatentsRepository patentsRepository;
	
	@Autowired
	private ScholarPatentsService scholarPatentsService;

	/**
	 * 分页查询专利
	 * @param neo4jPaperQ
	 * @return
	 */
	public Page<Neo4jPatents> getPapersByExpertId(Neo4jPatentsQ neo4jPatentsQ){
		
		/**
		 * 必要参数
		 */
		if(neo4jPatentsQ == null || neo4jPatentsQ.getExpertId() == null) {
			throw new MyException(ResponseCode.PARAMETER_REQUIRED);
		}
		
		Page<Neo4jPatents> pages = patentsRepository.getPatentsByExpertId(
    			neo4jPatentsQ.getExpertId(),
    			PageRequest.of(neo4jPatentsQ.getPageSkip(), neo4jPatentsQ.getPageSize()));
		
		
		if(pages!=null && pages.getContent()!=null) {
			
			List<Long> fIds = new ArrayList<>();
			
			for(Neo4jPatents ew : pages.getContent()) {
				fIds.add(ew.getfId());
			}
			
			if(fIds != null && fIds.size() > 0 ) {
				
				ScholarPatentsQ nQ = new ScholarPatentsQ();
				nQ.setfIds(fIds);
				List<ScholarPatents> cPatents = scholarPatentsService.queryList(nQ);
				
				Map<Long,ScholarPatents> cPatentsMap = null;
				
				if(cPatents != null && cPatents.size() > 0) {
					cPatentsMap = new HashMap<>();
					for(ScholarPatents n : cPatents) {
						cPatentsMap.put(n.getfId(), n);
					}
				}
				
				if(cPatentsMap != null) {
					for(Neo4jPatents ew_ : pages.getContent()) {
						covetMysqlToNeo4j(ew_, cPatentsMap.get(ew_.getfId()),false);
					}
				}
			}
			
		}
    	return pages;
	}

	/**
	 * 把mysql 的数据放到 neo4j中 
	 */
	private void covetMysqlToNeo4j(Neo4jPatents nO,ScholarPatents mO,Boolean isDetails) {
		
		if(mO == null) {
			logger.info("this data have no mysqlRecond :"+ nO.getfId());
			return;
		}
		
		nO.setfTitle(mO.getfTitle());
		nO.setfAuthors(mO.getfAuthors());
		nO.setUrl(mO.getUrl());
		
		if(isDetails) {
			nO.setfFos(mO.getfFos());
			nO.setfAbstract(mO.getfAbstract());
			nO.setWe(mO.getWe());
			nO.setLa(mO.getLa());
		}
	}
	
	public Neo4jPatents findOnePatents(Long id) {
		/**
		 * 从知识图谱中查询
		 */
		Neo4jPatents np = patentsRepository.findPatentsDetailById(id);
		
		if(np != null) {
			
			/**
			 * 从数据库中查出专利
			 */
			ScholarPatents scholarPatents = scholarPatentsService.queryOneById(np.getfId());
			
			if(scholarPatents != null) {
				covetMysqlToNeo4j(np, scholarPatents,true);
			}
			
		}
		return np;
	}
}
